const { getPage } = require("../../methods");
const { PARAMS_ERROR } = require("../../../utils/errorCode");

exports.main = (req, res) => {
    // 获取数据
    const {
        pageNo = 1,
        pageSize = 20,
        isActive,
        categoryId
    } = req.body
    if (!categoryId || categoryId < 0) {
        return res.err(PARAMS_ERROR, "分类id错误")
    }
    console.log(req.body);
    let sql = `select productId,productName,productPrice,productInventory,cover
               from products
               where isDelete = '0'
                 and categoryId = ${categoryId} limit ?,?;
    select count(*) count
    from products
    where isDelete = '0'; `;

    if (isActive && !['0', '1'].includes(isActive)) {
        return res.err(PARAMS_ERROR, "是否上架参数错误，应为 0 或 1");
    } else if (isActive === '1') {
        sql = `select productId,productName,productPrice,productInventory,cover
               from products
               where isDelete = 0
                 and isActive = 1
                 and categoryId = ${categoryId} limit ?,?;
        select count(*) count
        from products
        where isDelete = 0 and isActive = 1 and categoryId = ${categoryId}; `;
    } else if (isActive === '0') {
        sql = `select productId,productName,productPrice,productInventory,cover
               from products
               where isDelete = 0
                 and isActive = 0
                 and categoryId = ${categoryId} limit ?,?;
        select count(*) count
        from products
        where isDelete = 0 and isActive = 0 and categoryId = ${categoryId}; `;
    }


    return getPage(res, sql, pageNo, pageSize);
}